import { Notification } from '@arco-design/web-vue';

export function copy(content: string) {
    // Copy the content to clipboard
    if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(content).then(() => {
            Notification.success({
                title: '复制成功',
                content: '内容已复制到剪贴板',
                duration: 1000,
            });
        }).catch(() => {
            fallbackCopy(content);
        });
    } else {
        fallbackCopy(content);
    }
}

function fallbackCopy(content: string) {
    // Fallback method for copying to clipboard
    const textarea = document.createElement('textarea');
    textarea.value = content;
    textarea.style.position = 'fixed'; // Prevent scrolling to bottom
    textarea.style.opacity = '0';
    document.body.appendChild(textarea);
    textarea.select();
    try {
        document.execCommand('copy');
        Notification.success({
            title: '复制成功',
            content: '内容已复制到剪贴板',
            duration: 1000,
        });
    } catch (err) {
        Notification.error({
            title: '复制失败',
            content: '无法复制内容，请手动复制',
            duration: 1000,
        });
    } finally {
        document.body.removeChild(textarea);
    }
}


export function record(key: string, value: any) {
    // Record the value to local storage
    localStorage.setItem(key, JSON.stringify(value));
}

export function read(key: string) {
    // Read the value from local storage
    const value = localStorage.getItem(key);
    return value ? JSON.parse(value) : null;
}


export function hex2rgb2(hex: string) {
    // Convert hex color to rgb 255
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = (hexNumber >> 16) & 0xff;
    const g = (hexNumber >> 8) & 0xff;
    const b = hexNumber & 0xff;
    return `(${r}, ${g}, ${b})`;
}

export function hex2grb1(hex: string) {
    // Convert hex color to rgb 1
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = ((hexNumber >> 16) & 0xff) / 255;
    const g = ((hexNumber >> 8) & 0xff) / 255;
    const b = (hexNumber & 0xff) / 255;
    return `(${r.toFixed(3)}, ${g.toFixed(3)}, ${b.toFixed(3)})`;
}

export function hex2grb1Space(hex: string) {
    // Convert hex color to rgb 1
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = ((hexNumber >> 16) & 0xff) / 255;
    const g = ((hexNumber >> 8) & 0xff) / 255;
    const b = (hexNumber & 0xff) / 255;
    return `${r.toFixed(3)} ${g.toFixed(3)} ${b.toFixed(3)}`;
}

export function hex2hsv(hex: string) {
    // Convert hex color to hsv
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = (hexNumber >> 16) & 0xff;
    const g = (hexNumber >> 8) & 0xff;
    const b = hexNumber & 0xff;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h = 0;
    let s = 0;
    let v = max / 255;
    if (max !== 0) {
        s = (max - min) / max;
    }
    if (max === min) {
        h = 0;
    } else {
        switch (max) {
            case r:
                h = (g - b) / (max - min) + (g < b ? 6 : 0);
                break;
            case g:
                h = (b - r) / (max - min) + 2;
                break;
            case b:
                h = (r - g) / (max - min) + 4;
                break;
        }
        h /= 6;
    }
    return `(${h.toFixed(3)}, ${s.toFixed(3)}, ${v.toFixed(3)})`;
}


function hex2hsv1(hex: string) {
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = (hexNumber >> 16) & 0xff;
    const g = (hexNumber >> 8) & 0xff;
    const b = hexNumber & 0xff;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h = 0;
    let s = 0;
    let v = max / 255;
    if (max !== 0) {
        s = (max - min) / max;
    }
    if (max === min) {
        h = 0;
    } else {
        switch (max) {
            case r:
                h = (g - b) / (max - min) + (g < b ? 6 : 0);
                break;
            case g:
                h = (b - r) / (max - min) + 2;
                break;
            case b:
                h = (r - g) / (max - min) + 4;
                break;
        }
        h /= 6;
    }
    return [h, s, v];
}


export function rgb2hex(r: number, g: number, b: number){
    // Convert rgb 0 ~ 255 to hex
    return `#${((r << 16) + (g << 8) + b).toString(16).padStart(6, '0')}`;
}

export function rgb12hex(r: number, g: number, b: number){
    // Convert rgb 0 ~ 1 to hex
    const red = Math.round(r * 255);
    const green = Math.round(g * 255);
    const blue = Math.round(b * 255);
    return `#${((red << 16) + (green << 8) + blue).toString(16).padStart(6, '0')}`;
}

interface IColor {
    id: string;
    color: string;
    loc: number;
}

function hex2rgb(hex: string) {
    // Convert hex color to rgb 0 ~ 1
    const hexNumber = parseInt(hex.slice(1), 16);
    const r = ((hexNumber >> 16) & 0xff) / 255;
    const g = ((hexNumber >> 8) & 0xff) / 255;
    const b = (hexNumber & 0xff) / 255;
    return { r, g, b };
}

export function linear_interp_color(colors: IColor[], loc: number) {
    // Linear interpolation of color
    let start = colors[0];
    let end = colors[colors.length - 1];
    for (let i = 0; i < colors.length - 1; i++) {
        if (colors[i].loc <= loc && loc <= colors[i + 1].loc) {
            start = colors[i];
            end = colors[i + 1];
            break;
        }
    }
    const startColor = hex2rgb(start.color);
    const endColor = hex2rgb(end.color);
    const ratio = (loc - start.loc) / (end.loc - start.loc);
    const r = startColor.r + (endColor.r - startColor.r) * ratio;
    const g = startColor.g + (endColor.g - startColor.g) * ratio;
    const b = startColor.b + (endColor.b - startColor.b) * ratio;
    return rgb12hex(r, g, b);
}

export function hsv2hex(h: number, s: number, v: number) {
    // Convert hsv to hex
    let r = 0;
    let g = 0;
    let b = 0;
    let i = Math.floor(h * 6);
    let f = h * 6 - i;
    let p = v * (1 - s);
    let q = v * (1 - f * s);
    let t = v * (1 - (1 - f) * s);
    switch (i % 6) {
        case 0:
            r = v;
            g = t;
            b = p;
            break;
        case 1:
            r = q;
            g = v;
            b = p;
            break;
        case 2:
            r = p;
            g = v;
            b = t;
            break;
        case 3:
            r = p;
            g = q;
            b = v;
            break;
        case 4:
            r = t;
            g = p;
            b = v;
            break;
        case 5:
            r = v;
            g = p;
            b = q;
            break;
    }
    return rgb12hex(r, g, b);
}


// 调整颜色亮度，输入阈值为0-1，0为黑色，1为白色,0.5为原色
export function adjust_brightness(hex: string, factor: number) {
    const color = hex2hsv1(hex);
    const h = color[0];
    const s = color[1];
    const v = color[2];
    const new_v = v * factor;
    if (new_v > 1) {
        return hsv2hex(h, s, 1);
    }
    else if (new_v < 0) {
        return hsv2hex(h, s, 0);
    }
    else {
        return hsv2hex(h, s, new_v);
    }
}

// 调整颜色饱和度，输入阈值为0-1，0为灰色，1为原色
export function adjust_saturation(hex: string, factor: number) {
    const color = hex2hsv1(hex);
    const h = color[0];
    const s = color[1];
    const v = color[2];
    const new_s = s * factor;
    if (new_s > 1) {
        return hsv2hex(h, 1, v);
    }
    else if (new_s < 0) {
        return hsv2hex(h, 0, v);
    }
    else {
        return hsv2hex(h, new_s, v);
    }
}

// 获得相位不同的颜色，输入为0时为原色，0.5时为互补色，1时为原色
export function phase_color(hex: string, phase: number) {
    const color = hex2hsv1(hex);
    const h = color[0];
    const s = color[1];
    const v = color[2];
    const new_h = (h + phase) % 1;
    return hsv2hex(new_h, s, v);
}